package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.secsys.dto.StoFinanceassetdebtDTO;
import com.secsys.model.TbStoFinanceassetdebt;
import com.secsys.model.TbStoStock;
import com.secsys.service.StoFinanceAssetDebtService;
import com.secsys.service.StoStockService;

public class StoFinanceAssetDebtServiceImpl extends ServiceImpl<TbStoFinanceassetdebt, Integer> implements
		StoFinanceAssetDebtService {

	private StoStockService stoStockService;

	/**
	 * 根据股票ID查找StoFinanceassetdebtDTO（资产与负债）对象
	 * @param stockCode
	 * 			股票代码，TbStoStockDTO的stockCode
	 * @return
	 * 			tbStoFinanceassetdebt对象
	 */
	@Override
	public StoFinanceassetdebtDTO queryFinanceassetdebtByStockCode(String stockCode) {
		List<TbStoStock> list=stoStockService.findByProperty("from TbStoStock where stockCode=?", stockCode);
		if (null==list || list.size()==0) {
			return null;
		}
		TbStoStock tbStoStock=list.get(0);
		return queryFinanceassetdebtByTbStoStock(tbStoStock);
	}
	
	/**
	 * 根据股票ID查找StoFinanceassetdebtDTO（资产与负债）对象
	 * @param tbStoStock
	 * 			股票基本信息对象，tbStoStock
	 * @return
	 * 			tbStoFinanceassetdebt对象
	 */
	@Override
	public StoFinanceassetdebtDTO queryFinanceassetdebtByTbStoStock(TbStoStock tbStoStock) {
		
		String hql="from TbStoFinanceassetdebt as tsf where tsf.tbStoStock=?";
		//System.out.println(this.dao.findByProperty(hql, tbStoStock).size());
		//System.out.println(tbStoStock.getId());
		//return null;
		StoFinanceassetdebtDTO stoFinanceassetdebtDTO;
		if(this.dao.findByProperty(hql, tbStoStock).size()>0){
			stoFinanceassetdebtDTO=getDtoFromPojo(this.dao.findByProperty(hql, tbStoStock).get(0));
		}else{
			return null;
		}
		
		return stoFinanceassetdebtDTO;
	}
	
	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByDate(Date fieldDate,int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.fieldDate=?";
		List<TbStoFinanceassetdebt>	tbStoFinanceassetdebts= this.dao.findBySql(hql,new Object[]{fieldDate},offset,count);  
		if (tbStoFinanceassetdebts!=null && tbStoFinanceassetdebts.size()>0) {  
			 return forEachGetDtoFromPojo(tbStoFinanceassetdebts);
		}else {
			logger.info(hql+"param="+fieldDate);
			return null;
		}
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByDateRange(Date startDate,Date endDate,int offset, int count) {
//		String hql="from TbStoFinanceassetdebt as tsf where tsf.fieldDate between ? and ? ";  
//		List<TbStoFinanceassetdebt>	tbStoFinanceassetdebts = this.dao.findBySql(hql,new Object[]{startDate,endDate},offset,count);  
		
		String hql="from TbStoFinanceassetdebt as tsf1,TbStoFinanceassetdebt as tsf2 " +
				"where tsf1.tbStoStock=tsf2.tbStoStock and tsf1.fieldDate=? and tsf2.fieldDate=? " +
				" and  tsf1.totalAsset>tsf2.totalAsset*0.5 ";  
//		List<TbStoFinanceassetdebt>	tbStoFinanceassetdebts = this.dao.find(hql);    
		List<Object>  list= this.dao.findObject(hql);   
		List<TbStoFinanceassetdebt>	tbStoFinanceassetdebts =new ArrayList<TbStoFinanceassetdebt>();
		for (Object object : list) {
			TbStoFinanceassetdebt tbStoFinanceassetdebt=(TbStoFinanceassetdebt)((Object[])object)[0];
			tbStoFinanceassetdebts.add(tbStoFinanceassetdebt);
			System.out.println(tbStoFinanceassetdebt); 
		}
		if (tbStoFinanceassetdebts!=null && tbStoFinanceassetdebts.size()>0) {  
			 return forEachGetDtoFromPojo(tbStoFinanceassetdebts);
		}else {
			System.err.println("null-->queryFinanceassetdebtByDate(Date startDate,Date endDate)");
			logger.info(hql+"param="+startDate+","+endDate);
			return null;
		}
	}


	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByAccountReceivableOtherRange(double startAccountReceivableOther,
			double endAccountReceivableOther, int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.accountReceivableOther between ? and ?";  
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startAccountReceivableOther,endAccountReceivableOther},offset,count);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByAccountReceivableRange(double startAccountReceivable,double endAccountReceivable, int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.accountReceivable between ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startAccountReceivable,endAccountReceivable},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByBadAccountReadyRange(double startBadAccountReady,double endBadAccountReady, int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.badAccountReady between ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startBadAccountReady,endBadAccountReady},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByCoinFundRange(double startCoinFund,double endCoinFund, int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.coinFund between ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startCoinFund,endCoinFund},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByFlowIndebtedRange(double startFlowIndebted,double endFlowIndebted, int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.flowIndebted ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startFlowIndebted,endFlowIndebted},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByLongTermIndebtedRange(
			double startLongTermIndebted,double endLongTermIndebted, int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.longTermIndebted ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startLongTermIndebted,endLongTermIndebted},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByPartnerProfitRange(
			double startPartnerProfit,double endPartnerProfit,int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.partnerProfit ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startPartnerProfit,endPartnerProfit},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByTotalIndebtedRange(
			double startTotalIndebted,double endTotalIndebted, int offset, int count) {
		String hql="from TbStoFinanceassetdebt as tsf where tsf.totalIndebted ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startTotalIndebted,endTotalIndebted},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryFinanceassetdebtByTotalAssetRange(double startTotalAsset,double endTotalAsset,int offset,int count){
		String hql="from TbStoFinanceassetdebt as tsf where tsf.totalAsset ? and ?";
		List<TbStoFinanceassetdebt> tbStoFinanceassetdebtList=this.dao.findBySql(hql,new Object[]{startTotalAsset,endTotalAsset},count,offset);
		return forEachGetDtoFromPojo(tbStoFinanceassetdebtList);
	}

	/**
	 * @Description 将持久化的pojo转换成DTO领域对象
	 * @param tbStoFinanceassetdebt
	 * 			持久化的TbStoFinanceassetdebt对象
	 * @return
	 * 			StoFinanceassetdebtDTO领域对象
	 */ 
	private StoFinanceassetdebtDTO getDtoFromPojo(TbStoFinanceassetdebt tbStoFinanceassetdebt){
		StoFinanceassetdebtDTO stoFinanceassetdebtDTO=new StoFinanceassetdebtDTO(
			tbStoFinanceassetdebt.getId(),tbStoFinanceassetdebt.getFieldDate(),
			tbStoFinanceassetdebt.getTotalAsset(),tbStoFinanceassetdebt.getTotalIndebted(),
			tbStoFinanceassetdebt.getFlowIndebted(),tbStoFinanceassetdebt.getLongTermIndebted(),
			tbStoFinanceassetdebt.getCoinFund(),tbStoFinanceassetdebt.getAccountReceivable(),
			tbStoFinanceassetdebt.getAccountReceivableOther(),tbStoFinanceassetdebt.getBadAccountReady(),
			tbStoFinanceassetdebt.getPartnerProfit()
		);
		return stoFinanceassetdebtDTO;
	}
	
	/**
	 * @Description 批量地将持久化的pojo对象TbStoFinanceassetdebt 集合转换成领域对象 StoFinanceassetdebtDTO，并存放的领域对象list里面dtoList
	 * @param stoList
	 * 			持久化POJO对象List<TbStoFinanceassetdebt> 
	 * @return List<StoFinanceassetdebtDTO>
	 * 			领域对象List<StoFinanceassetdebtDTO> 
	 */
	private List<StoFinanceassetdebtDTO> forEachGetDtoFromPojo(List<TbStoFinanceassetdebt> stoList) {
		List<StoFinanceassetdebtDTO> dtoList=new ArrayList<StoFinanceassetdebtDTO>();
		for(TbStoFinanceassetdebt tbStoFinanceassetdebt:stoList){
			dtoList.add(getDtoFromPojo(tbStoFinanceassetdebt)); 
		}
		return dtoList;
	}

	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}

	@Override
	public List<StoFinanceassetdebtDTO> queryAssetdebtListByStockCode(
			String stockCode) {
		// TODO Auto-generated method stub
		List<TbStoStock> list=stoStockService.findByProperty("from TbStoStock where stockCode=?", stockCode);
		if (null==list || list.size()==0) {
			return null;
		}
		TbStoStock tbStoStock=list.get(0);
		String hql="from TbStoFinanceassetdebt as tsf where tsf.tbStoStock=?";
		return forEachGetDtoFromPojo(this.dao.findByProperty(hql, tbStoStock));
	}
}
